﻿@using SH = EMoney.IStock.Client.Framework.StringHelper;

@using EMoney.IStock.Client.Models.CQ;

@{
    Layout = null;

    string logFlag = "0";
    string strGuessDate = string.Empty;
    DateTime guessDate = ViewBag.GuessDate;
    bool canGuess = ViewBag.CanGuess;

    string userName = string.Empty;
    string userPicUrl = string.Empty;
    int curUserID = 0;

    int guessTotalCount = 0;
    int correctGuessCount = 0;
    int continueCorrectGuessCount = 0;
    int continueErrorGuessCount = 0;
    string continueCorrectTxt = "--";
    string continueErrorTxt = "--";
    int myGuessType = -1;
    int? myGuessResult = null;
    decimal ratio = 1;
    DateTime myGuessDate = DateTime.MinValue;

    if (ViewBag.MyGuess != null)
    {
        MyGuess myGuess = ViewBag.MyGuess;

        logFlag = "1";
        userName = myGuess.UserName;
        userPicUrl = myGuess.UserPicUrl;

        curUserID = myGuess.UserID;
        guessTotalCount = myGuess.GuessTotalCount;
        correctGuessCount = myGuess.CorrectGuessCount;
        continueCorrectGuessCount = myGuess.ContinueCorrectGuessCount;
        continueErrorGuessCount = myGuess.ContinueErrorGuessCount;

        if (continueCorrectGuessCount >= 2)
        {
            continueCorrectTxt = string.Concat(continueCorrectGuessCount, " 次");
        }
        if (continueErrorGuessCount >= 2)
        {
            continueErrorTxt = string.Concat(continueErrorGuessCount, " 次");
        }

        myGuessDate = myGuess.TradeDate;
        myGuessType = myGuess.GuessType;
        myGuessResult = myGuess.GuessResult;

        GuessType guessType = myGuess.GuessTypeStat;

        if (guessType != null)
        {
            int sum = guessType.DownCount + guessType.UpCount;

            switch (myGuessType)
            {
                case 1:
                    ratio = (decimal)guessType.UpCount / sum;
                    break;
                case 0:
                    ratio = (decimal)guessType.DownCount / sum;
                    break;
                default:
                    break;
            }
        }
    }

    ratio = ratio * 100;

    GuessTotal guessTotal = ViewBag.GuessTotal;

    DateTime? statTime = null;
    int totalCount = 0;
    if (guessTotal != null)
    {
        statTime = guessTotal.StatTime;
        totalCount = guessTotal.Count;
    }

    int preCorrectRatio = 0;
    int preErrorRatio = 0;
    GuessResult guessResult = ViewBag.PreGuessResult;   //上一交易日竞猜结果

    if (guessResult != null)
    {
        if (guessResult.ErrorCount == 0 || guessResult.CorrectCount == 0)
        {
            if (guessResult.ErrorCount == 0)
            {
                preCorrectRatio = 100;
            }
            else
            {
                preErrorRatio = 100;
            }
        }
        else
        {
            preCorrectRatio = decimal.ToInt32((decimal)guessResult.CorrectCount / (guessResult.ErrorCount + guessResult.CorrectCount) * 100);
            preErrorRatio = 100 - preCorrectRatio;
        }
    }

    //int promotionRatio = 0;
    //int outRatio = 0;
    decimal promotionRatio = 0m;
    decimal outRatio = 0m;
    PromotionOutCount promotionOutCount = ViewBag.PromotionOutCount;    //晋级、出局人数

    if (promotionOutCount != null)
    {
        if (promotionOutCount.OutCount == 0 || promotionOutCount.PromotionCount == 0)
        {
            if (promotionOutCount.OutCount == 0)
            {
                promotionRatio = 100;
            }
            else
            {
                outRatio = 100;
            }
        }
        else
        {
            //promotionRatio = decimal.ToInt32((decimal)promotionOutCount.PromotionCount / (promotionOutCount.PromotionCount + promotionOutCount.OutCount) * 100);
            promotionRatio = (decimal)promotionOutCount.PromotionCount / (promotionOutCount.PromotionCount + promotionOutCount.OutCount) * 100;
            outRatio = 100 - promotionRatio;
        }
    }

    TimeSpan timespan = guessDate - Convert.ToDateTime("2014-10-15");

    int guessDays = timespan.Days - 1;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>我是传奇</title>
    <link rel="stylesheet" type="text/css" href="@SH.GetStaticFile("IStockClientStatic/cq/css/style.css")" id="linkcss" />
    <script src="http://static.emoney.cn/js/libs/jquery.min.1.8.3.js" type="text/javascript"></script>
</head>
<body>
    <div class="box">
        <div class="dw">
            <div class="b_1">
                <div class="clear"></div>
                @{
                    if (ViewBag.CQGuess != null && canGuess == true)
                    {
                        if (myGuessDate != null && myGuessDate.Date != DateTime.MinValue.Date)
                        {
                            <div class="t_12">您竞猜 <span class="b_1_1">@SH.FormatDateTime(myGuessDate, "M月d日")</span> 大盘涨跌：<span class="@SH.GetGuessClass(myGuessType)">@SH.FormatGuessType(myGuessType)</span><br />竞猜结果：<span class="@SH.GetGuessClass(myGuessResult.Value)">@SH.FormatGuessResult(myGuessResult.Value)</span></div>
                            <div class="btn_3"><a href="javascript:;" class="showCQ" userid="@ViewBag.CQGuess.UserID" username="@ViewBag.CQGuess.UserName" days="@ViewBag.CQGuess.ContinueCorrectGuessCount" beat="@(totalCount - 1)">大奖已产生，点击查看</a></div>
                        }
                        else
                        {
                            if (curUserID == ViewBag.CQGuess.UserID)
                            {
                                <div class="t_11" style="margin-top:15px;">恭喜您获得最终大奖<br />
                                <span style="font-size:14px;">连续猜对大盘 @ViewBag.CQGuess.ContinueCorrectGuessCount 次，击败 @(totalCount - 1) 名竞争对手！</span></div>
                            }
                            else
                            {
                                <div class="t_11" style="margin-top:15px;">我是传奇获奖得主： <span class="b_1_1" title='@ViewBag.CQGuess.UserName'>@SH.StringSubstrLen(ViewBag.CQGuess.UserName, 10)</span><br /> 
                                <span style="font-size:14px;">连续猜对大盘 @ViewBag.CQGuess.ContinueCorrectGuessCount 次，击败 @(totalCount - 1) 名竞争对手！</span></div>
                            }
                        
                            <div class="b_1_3" style="color:red;font-size:16px">奖品：价值2880元爱炒股奖品一份！</div>
                        }
                    }
                    else if (promotionOutCount != null && promotionOutCount.PromotionCount == 0 && canGuess == true)
                    { 
                        <div class="t_11">活动结束！本期“传奇”选手未诞生！</div>
                    }
                    else
                    {
                        if (!canGuess && guessDate != myGuessDate)
                        {
                            <div class="t_11">竞猜时间已结束，请15:30分后再来</div>
                        }
                        else
                        {
                            if ((ViewBag.CQGuess != null && canGuess == true) || (myGuessDate != null && myGuessDate.Date != DateTime.MinValue.Date && myGuessDate.Date != guessDate && myGuessResult != null))   //最新的竞猜结果
                            {
                                if (ViewBag.CQGuess != null && canGuess == true)
                                {
                                    if (myGuessDate == null || myGuessDate.Date == DateTime.MinValue.Date)
                                    {
                                        if (curUserID == ViewBag.CQGuess.UserID)
                                        {
                                            <div class="t_11" style="margin-top:15px;">恭喜您获得最终大奖<br />
                                            <span style="font-size:14px;">连续猜对大盘 @ViewBag.CQGuess.ContinueCorrectGuessCount 次，击败 @(totalCount - 1) 名竞争对手！</span></div>
                                        }
                                        else
                                        {
                                            <div class="t_11" style="margin-top:15px;">我是传奇获奖得主： <span class="b_1_1" title='@ViewBag.CQGuess.UserName'>@SH.StringSubstrLen(ViewBag.CQGuess.UserName, 10)</span><br /> 
                                            <span style="font-size:14px;">连续猜对大盘 @ViewBag.CQGuess.ContinueCorrectGuessCount 次，击败 @(totalCount - 1) 名竞争对手！</span></div>
                                        }                                        
                                        
                                        <div class="b_1_3" style="color:red;font-size:16px">奖品：价值2880元爱炒股奖品一份！</div> 
                                    }
                                    else
                                    {
                                        <div class="btn_3"><a href="javascript:;" class="showCQ" userid="@ViewBag.CQGuess.UserID" username="@ViewBag.CQGuess.UserName" days="@ViewBag.CQGuess.ContinueCorrectGuessCount" beat="@(totalCount - 1)">大奖已产生，点击查看</a></div>
                                    }
                                }
                                else
                                {
                                    if (promotionOutCount != null && promotionOutCount.PromotionCount == 0 && canGuess == true)
                                    { 
                                        <div class="t_11">活动结束！本期“传奇”选手未诞生！</div>
                                    }
                                    else
                                    {
                                        if (myGuessDate != null && myGuessDate.Date != DateTime.MinValue.Date)
                                        {
                                            <div class="t_12">您竞猜 <span class="b_1_1">@SH.FormatDateTime(myGuessDate, "M月d日")</span> 大盘涨跌：<span class="@SH.GetGuessClass(myGuessType)">@SH.FormatGuessType(myGuessType)</span><br />竞猜结果：<span class="@SH.GetGuessClass(myGuessResult.Value)">@SH.FormatGuessResult(myGuessResult.Value)</span></div>
                                        }
                                        <div class="btn_3"><a href="javascript:;" class="showNextGuessDate">点击竞猜下一个交易日</a></div>
                                    }
                                }
                            }
                            else if (guessDate == myGuessDate || myGuessDate == null)  //已竞猜最新的交易日 或 最新的竞猜未出结果时
                            {
                                <div class="t_11">您竞猜 <span class="b_1_1">@SH.FormatDateTime(myGuessDate, "M月d日")</span> 大盘涨跌：<span class="@SH.GetGuessClass(myGuessType)">@SH.FormatGuessType(myGuessType)</span><br /></div>
		                        <div class="b_1_3"><span style="color:red;">@SH.FormatDecimal(ratio, "0")%</span>的竞猜者与您的观点相同，15:30分后查看结果!</div>
                            }
                            else
                            {
                                if (promotionOutCount != null && promotionOutCount.PromotionCount == 0)
                                { 
                                    <div class="t_11">活动结束！本期“传奇”选手未诞生！</div>
                                }
                                else
                                {
                                    <p class="t_1">竞猜 <span>@SH.FormatDateTime(guessDate, "M月d日")</span> 大盘涨跌</p>
                                    <p class="t_2"><a href="javascript:;" class="btn_1 guess" flag="1" hidefocus="true">看涨</a><a href="javascript:;" class="btn_2 guess" flag="0" hidefocus="true">看跌</a></p>
                                }
                            }
                        }
                    }
                }
                <div class="clear"></div>
            </div>
            <div class="b_2">
                @{
                    if (ViewBag.MyGuess == null)
                    {
                        <p class="t_3">您还没有<a href="javascript:;" class="login" hidefocus="true">登录</a>，登录后便可以竞猜</p>
                        <div class="clear"></div>
                    }
                    else
                    {
                        <div class="b_2_1">
                             <div class="cover"></div>
                             <div class="user_pic">
                                <img src="@userPicUrl" style="width:68px;height:68px;" alt="" />
                             </div>
                             <p title="@userName">@SH.StringSubstrLen(userName, 8)</p>
		                 </div>
		                 <div class="b_2_2">
		                     <span>我的战绩</span><br />
		                     总竞猜数：<span id="spanGuessTotalCount">@guessTotalCount</span> 次<br />
                             猜中次数：@correctGuessCount 次<br />
                             连续猜对：@continueCorrectTxt<br />
                             连续猜错：@continueErrorTxt
		                 </div>
		                 <div class="clear"></div>
                    }
                    }
            </div>
            <div class="b_3">
                @{
                    if (promotionOutCount != null)
                    {
                        <p class="t_4">
                            @{
                        if (promotionOutCount != null)
                        {
                                    @:目前晋级人数
                                }
                        else
                        {
                                    @:统计信息盘后更新
                                }
                                }
                        </p>
                        <p class="t_5">
                            @{
                        if (promotionOutCount != null)
                        {
                                    @promotionOutCount.PromotionCount @:人
                                }
                                }
                        </p>
                        <div class="jc">
                            <div class="clear"></div>
                            <div class="left">
                                @{
                        if (guessResult != null)
                        {                                
                                    <div class="left_1" id="divPreResult"></div>
                                    <div class="left_2" style="color:#3c3c3c;">@SH.FormatDateTime(guessResult.TradeDate, "M月d日")竞猜结果</div>
                        }
                                }
                            </div>
                            <div class="right">
                                @{
                        if (promotionOutCount != null)
                        {                                
                                        <div class="right_1" id="divPromotionOut"></div>
                                        <div class="left_2" style="color:#3c3c3c;">总竞猜人数</div> 
                        }
                                }
                            </div>
                            <div class="clear"></div>
                        </div>
                    }
                    else
                    {
                        <div class="cq"></div>
                    }
                    }
                <p class="t_6" style="margin-top:-2px;">
                @{
                    if (statTime != null)
                    {
                        <span>截至 @SH.FormatDateTime(statTime.Value, "M-d H:mm") 参与竞猜总人数 @totalCount 人</span>        
                    }
                }  
                </p>
            </div>
            <div class="b_4">
                <div class="t_7">活动规则</div>
                <div class="t_8">
                    竞猜时间：交易日15:30~下一交易日9:15<br />
                    竞猜规则：竞猜上证指数的涨跌，连续猜对者晋级猜错者出局....<span class="t_9"><a href="http://win.emoney.cn/Discuss/df92f31e88d6b045" target="_blank" hidefocus="true">>>查看更多</a></span><br />
                    奖项设置：<br />
                    传奇奖：价值2980元主力版策略包<br />
                    <a href="http://shop.emoney.cn/product/15_12.shtml?token=xxx" target="_blank" class="t_10" hidefocus="true">（点击查看奖品）</a>；<br />
                    鼓励奖：爱炒股操盘线30天；<br />
                    参与奖：爱炒股鹰眼监控15天
                </div>
                <div class="t_8" style="color:#E65246;">当前竞猜第 @guessDays 个交易日</div>
            </div>
        </div>
    </div>
    <input type="hidden" id="hidIsLogin" value="@logFlag" />
    <input type="hidden" id="hidCurUserID" value="@curUserID" />
    <input type="hidden" id="hidGuessDate" value="@SH.FormatDateTime(guessDate, "yyyy-M-d")" />
    <input type="hidden" id="hidGuessDateShow" value="@SH.FormatDateTime(guessDate, "M月d日")" />
</body>
    <script src="@SH.GetStaticFile("IStockClientStatic/js/istockfunc.js")" type="text/javascript"></script>
    <script src="@SH.GetStaticFile("IStockClientStatic/js/highcharts.js")" type="text/javascript"></script>
    <script src="@SH.GetStaticFile("IStockClientStatic/js/HStheme0.js")" type="text/javascript"></script>
</html>
<script type="text/javascript">
    var www = "@Url.Content("~/")", canGuess = "@canGuess", preCorrect = "@preCorrectRatio", preError = "@preErrorRatio", promotionRatio = "@promotionRatio", outRatio = "@outRatio";

    $(document).ready(function () {
        $(".login").click(function () {
            ISTOCK_FUNC_LOGIN();
        });

        $(".b_1").delegate('.guess', 'click', '', function () {
            if ($("#hidIsLogin").val() != "1") {
                ISTOCK_FUNC_LOGIN();
                return;
            }
            var flag = $(this).attr("flag");
            var guessDate = $("#hidGuessDate").val();

            if (flag != "" && guessDate != "") {
                $.ajax({
                    type: "post",
                    url: www + "cq/guess",
                    data: { flag: flag, date: guessDate },
                    datatype: "text",
                    success: function (data) {
                        switch (data) {
                            case "1":
                                window.location.href = www + "cq";
                                break;
                            default:
                                break;
                        }
                    },
                    beforeSend: function () {
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                    }
                });
            }
        });

        $(".showNextGuessDate").click(function () {
            var showTxt = "<div class=\"clear\"></div>";

            if (canGuess == "True" || canGuess == "true") {
                showTxt += "<p class=\"t_1\">竞猜 <span>" + $("#hidGuessDateShow").val() + "</span> 大盘涨跌</p>";
                showTxt += "<p class=\"t_2\"><a href=\"javascript:;\" class=\"btn_1 guess\" flag=\"1\" hidefocus=\"true\">看涨</a><a href=\"javascript:;\" class=\"btn_2 guess\" flag=\"0\" hidefocus=\"true\">看跌</a></p>";
            }
            else {
                showTxt += "<div class=\"t_11\">竞猜时间已结束，请15:30分后再来</div>";
            }

            showTxt += "<div class=\"clear\"></div>";

            $(".b_1").html(showTxt);
        });

        $(".showCQ").click(function () {
            var userid = $(this).attr("userid");
            var days = $(this).attr("days");
            var beat = $(this).attr("beat");
            var username = $(this).attr("username");
            var showTxt = "<div class=\"clear\"></div>";

            if (userid == $("#hidCurUserID").val()) {
                showTxt += "<div class=\"t_11\" style=\"margin-top:15px;\">恭喜您获得最终大奖<br />";
            }
            else {
                if (username != undefined && username != "") {
                    if (username.length > 8) {
                        username = username.substring(0, 8) + "...";
                    }
                }

                showTxt += "<div class=\"t_11\" style=\"margin-top:15px;\">我是传奇获奖得主： <span class=\"b_1_1\" title=\"" + $(this).attr("username") + "\">" + username + "</span><br />";
            }
            showTxt += "<span style=\"font-size:14px;\">连续猜对大盘 " + days + " 次，击败 " + beat + " 名竞争对手！</span></div>";
            showTxt += "<div class=\"b_1_3\" style=\"color:red;font-size:16px\">奖品：价值2880元爱炒股奖品一份！</div>";

            showTxt += "<div class=\"clear\"></div>";

            $(".b_1").html(showTxt);
        });

        if (preCorrect != "0" || preError != "0") {
            initPie("divPreResult", "胜", "负", parseInt(preCorrect), parseInt(preError));
        }

        if (promotionRatio != "0" || outRatio != "0") {
            initPie("divPromotionOut", "晋级", "出局", parseFloat(parseFloat(promotionRatio).toFixed(2)), parseFloat(parseFloat(outRatio).toFixed(2)));
            //initPie("divPromotionOut", "晋级", "出局", parseInt(promotionRatio), parseInt(outRatio));
        }
    });

    function initPie(id, txt1, txt2, success, fail) {
        var chart = new Highcharts.Chart({
            chart: {
                renderTo: id,
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                marginBottom: 25,
                marginTop: 0,
                padding: 0
            },
            credits: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            title: {
                text: ''
            },
            subtitle: {
                text: ''
            },

            tooltip: {
                enabled: true,
                shared: true,
                crosshairs: true,
                pointFormat: '{series.name}: <b>{point.percentage}%</b>',
                percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: false,
                    //cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        useHTML: true,
                        distance: -20,
                        color: '#ffffff',
                        style: {
                            fontsize: '14px',
                            fontfamily: 'arial,微软雅黑',
                            textalign: 'center'
                        },
                        formatter: function () {
                            if (this.y >= 20) {
                                return ' <span style="color:#fff;font-size:14px;">' + this.y + '%</span> '
                            }
                        }
                    },
                    showInLegend: true,
                    shadow: false,
                    size: 95,
                    point: {
                        events: {
                            legendItemClick: function () { return false; }// false
                        }
                    }
                }
            },
            legend: {
                enabled: true,
                floating: true,
                itemWidth: 40,
                maxHeight: 25,
                margin: 10,
                verticalAlign: 'bottom',
                symbolWidth: 9,
                layout: 'horizontal',
                y: 12
            },
            series: [{
                type: 'pie',
                name: '',
                animation: false,
                marker: {
                    enabled: false
                },
                data: [
                    [txt1, success],
                    [txt2, fail]
                ]
            }]
        });
    }
</script>